home *** CD-ROM | disk | FTP | other *** search
/ Enter 2006 September / Enter 09 2006.iso / Internet / SpamExperts Home 1.1 / SpamExperts Home.exe / lib / spamexperts.modules / encodings / __init__.pyc (.txt) next >
Encoding:
Python Compiled Bytecode  |  2006-07-14  |  3.7 KB  |  127 lines

  1. # Source Generated with Decompyle++
  2. # File: in.pyc (Python 2.4)
  3.  
  4. ''' Standard "encodings" Package
  5.  
  6.     Standard Python encoding modules are stored in this package
  7.     directory.
  8.  
  9.     Codec modules must have names corresponding to normalized encoding
  10.     names as defined in the normalize_encoding() function below, e.g.
  11.     \'utf-8\' must be implemented by the module \'utf_8.py\'.
  12.  
  13.     Each codec module must export the following interface:
  14.  
  15.     * getregentry() -> (encoder, decoder, stream_reader, stream_writer)
  16.     The getregentry() API must return callable objects which adhere to
  17.     the Python Codec Interface Standard.
  18.  
  19.     In addition, a module may optionally also define the following
  20.     APIs which are then used by the package\'s codec search function:
  21.  
  22.     * getaliases() -> sequence of encoding name strings to use as aliases
  23.  
  24.     Alias names returned by getaliases() must be normalized encoding
  25.     names as defined by normalize_encoding().
  26.  
  27. Written by Marc-Andre Lemburg (mal@lemburg.com).
  28.  
  29. (c) Copyright CNRI, All Rights Reserved. NO WARRANTY.
  30.  
  31. '''
  32. import codecs
  33. import exceptions
  34. import types
  35. import aliases
  36. _cache = { }
  37. _unknown = '--unknown--'
  38. _import_tail = [
  39.     '*']
  40. _norm_encoding_map = '                                              . 0123456789       ABCDEFGHIJKLMNOPQRSTUVWXYZ      abcdefghijklmnopqrstuvwxyz                                                                                                                                     '
  41. _aliases = aliases.aliases
  42.  
  43. class CodecRegistryError(exceptions.LookupError, exceptions.SystemError):
  44.     pass
  45.  
  46.  
  47. def normalize_encoding(encoding):
  48.     """ Normalize an encoding name.
  49.  
  50.         Normalization works as follows: all non-alphanumeric
  51.         characters except the dot used for Python package names are
  52.         collapsed and replaced with a single underscore, e.g. '  -;#'
  53.         becomes '_'. Leading and trailing underscores are removed.
  54.  
  55.         Note that encoding names should be ASCII only; if they do use
  56.         non-ASCII characters, these must be Latin-1 compatible.
  57.  
  58.     """
  59.     if type(encoding) is types.UnicodeType:
  60.         encoding = encoding.encode('latin-1')
  61.     
  62.     return '_'.join(encoding.translate(_norm_encoding_map).split())
  63.  
  64.  
  65. def search_function(encoding):
  66.     entry = _cache.get(encoding, _unknown)
  67.     if entry is not _unknown:
  68.         return entry
  69.     
  70.     norm_encoding = normalize_encoding(encoding)
  71.     if not _aliases.get(norm_encoding):
  72.         pass
  73.     aliased_encoding = _aliases.get(norm_encoding.replace('.', '_'))
  74.     if aliased_encoding is not None:
  75.         modnames = [
  76.             aliased_encoding,
  77.             norm_encoding]
  78.     else:
  79.         modnames = [
  80.             norm_encoding]
  81.     for modname in modnames:
  82.         if not modname:
  83.             continue
  84.         
  85.         
  86.         try:
  87.             mod = __import__(modname, globals(), locals(), _import_tail)
  88.         except ImportError:
  89.             continue
  90.  
  91.     else:
  92.         mod = None
  93.     
  94.     try:
  95.         getregentry = mod.getregentry
  96.     except AttributeError:
  97.         mod = None
  98.  
  99.     if mod is None:
  100.         _cache[encoding] = None
  101.         return None
  102.     
  103.     entry = tuple(getregentry())
  104.     if len(entry) != 4:
  105.         raise CodecRegistryError, 'module "%s" (%s) failed to register' % (mod.__name__, mod.__file__)
  106.     
  107.     for obj in entry:
  108.         if not callable(obj):
  109.             raise CodecRegistryError, 'incompatible codecs in module "%s" (%s)' % (mod.__name__, mod.__file__)
  110.             continue
  111.     
  112.     _cache[encoding] = entry
  113.     
  114.     try:
  115.         codecaliases = mod.getaliases()
  116.     except AttributeError:
  117.         pass
  118.  
  119.     for alias in codecaliases:
  120.         if not _aliases.has_key(alias):
  121.             _aliases[alias] = modname
  122.             continue
  123.     
  124.     return entry
  125.  
  126. codecs.register(search_function)
  127.